package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.entity.*;
import com.dobby.pea.service.GoodService;
import com.dobby.pea.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/good")
@Slf4j
public class GoodController {

    @Autowired
    private GoodService goodService;

    /**
     * 返回一个用户旗下的所有商品 //前端输入用户id，当用户id等于商品createUser字段，返回所有商品信息
     * @param page
     * @param pageSize
     * @param name
     * @param typeId
     * @param userId
     * @return
     */
    @GetMapping("/userAllGood")
    public R<Page> userAllGoodPage(int page, int pageSize,String name,Long typeId,Long userId){

        log.info("page = {},pageSize = {},name = {},typeId = {}" ,page,pageSize,name,typeId);

        return goodService.GoodDtoByUserPage(page,pageSize,name,typeId,userId);

    }

    /**
     * 返回每个二级类别的商品热门销量排名
     * @return
     */
    @GetMapping("/sortHotGood")
    private R<List<tbGood>> sortHotGood(){
        log.info("返回一级类别的热门商品");
        return null;
    }


    /**
     * 获取所有商品
     * @return
     */
    @GetMapping("/all")
    public R<List<tbGood>> allGood(){
        log.info("查询所有商品");

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();

        List<tbGood> list = goodService.list(queryWrapper);

        return R.success(list);

    }


    /**
     * 商品信息分页查询,商品名字段模糊搜索+商品类型id搜索
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name,Long typeId){

        log.info("page = {},pageSize = {},name = {},typeId = {}" ,page,pageSize,name,typeId);

       return goodService.GoodDtoPage(page,pageSize,name,typeId);

    }

    /**
     * 新增商品
     * @param good
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody tbGood good){
        log.info(good.toString());

        goodService.save(good);
        goodService.initSales(good);

        return R.success(String.valueOf(good.getId()));
    }


    /**
     * 新增商品图片
     * @param goodImg
     * @return
     */
    @PostMapping("/img")
    public R<String> saveImg(@RequestBody List<tbGoodImg> goodImg){
        log.info(goodImg.toString());

        return goodService.saveImg(goodImg);
    }


    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<tbGood> get(@PathVariable Long id){
        log.info("根据id查询商品信息...{}",id);
        return goodService.getGoodById(id);
    }


    /**
     * 更新商品
     * @param good
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody tbGood good){
        log.info(good.toString());

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.in(tbGood::getId,good.getId());

        int count = goodService.count(queryWrapper);

        if (count<=0){
            return R.error("商品未查询到");
        }else {
            goodService.updateById(good);
            return R.success("商品信息修改成功");
        }

    }

    /**
     * 下架(可批量)商品 修改数据库字段中的is_deleted 为 1，逻辑删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> soldOut(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        return goodService.soldOutGood(ids);

    }

    /**
     * 数据库删除(可批量)商品
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        return goodService.deleteGood(ids);

    }


    /**
     * 根据类型id查询数据当前类型下的商品
     * @param typeid
     * @return
     */
    @GetMapping(params ={"typeid"})
    public R<List<tbGood>> getType(@RequestParam Long typeid){
        log.info("根据typeid查询商品全部分类信息...");

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(tbGood::getTypeId,typeid);

        List<tbGood> list = goodService.list(queryWrapper);
        if(list != null){
            return R.success(list);
        }
        return R.error("没有查询到对应商品信息");
    }




}
